<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>短信号码问题查找</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
<link href="../../css/style.css" rel="stylesheet" type="text/css">
<link href="../../css/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css">
<script src="../../layui/layui.js"></script>
<script src="../../js/jquery-1.8.3.js"></script>
<script src="../../js/util.js"></script>
<script src="../../js/jquery-ui-1.10.4.custom.min.js"></script>
<script>
	layui.use([ 'table', 'form', 'jquery', 'element', 'layer','laydate' ], function() {
		var table = layui.table;
		var form = layui.form;
		var $ = layui.$;
		var layer = layui.layer;
		var user;
		var mgrNameArr=[];
        var mgrIdArr=[];
		var laydate = layui.laydate;

		 laydate.render({
	             elem: '#startTime'
	             , type: 'datetime'
	             
	        });
	        laydate.render({
	             elem: '#endTime'
	             , type: 'datetime'
	             
	        });
	        
		var options = {
		
			elem : "#table",
			id : "modelReload",
			url : '/check/num',
			loading: true, //翻页加loading
			method : 'post',
			request : {
				pageName : 'page',
				limitName : 'limit'
			},
			response : {
				statusName : 'code',
				statusCode : 0,
				msgName : 'msg',
				countName : 'count',
				dataName : 'data'
			},
			page : {
				count : "count",
				limit : 10,
				limits : [ 10, 20, 50 ],
				layout : [ 'prev', 'page', 'next', 'skip', 'limit', 'count' ],
				prev : "上一页",
				next : "下一页",
				groups : 5
			},
			cols : [ [ //表头
				{
					field : 'belong',
					title : '地市/区县'
				},
				{
					field : 'name',
					title : '项目名称'
				},
				{
					field : 'msisdn',
					title : '号码'
				},
				{
					field : 'stateName',
					title : '项目状态'
				},
				{
					field : 'templateStateName',
					title : '模板状态'
				},
				{
					field : 'stationNum',
					title : '基站数量'
				},
				{
					field : 'dataSources',
					title : 'IT是否提供数据'
				}
				,
				{
					field : 'iscz',
					title : '是否常驻'
				}
				,
				{
					field : 'isSeven',
					title : '是否七日已发(上次发送时间)'
				}
				,
				{
					field : 'isBlack',
					title : '是否黑名单'
				}
				,
				{
					field : 'smSend',
					title : '是否入待发表'
				}
				,
				{
					field : 'deail',
					title : '是否已发'
				}
				,
				{
					field : 'reason',
					title : '原因'
				}
			] ],
			done : function(res) {
				layer.close(index); 
			}
		};
		var options2 = {
			elem : "#table",
			id : "modelReload2",
			url : '/check/mgr',
			method : 'post',
			request : {
				pageName : 'page',
				limitName : 'limit'
			},
			response : {
				statusName : 'code',
				statusCode : 0,
				msgName : 'msg',
				countName : 'count',
				dataName : 'data'
			},
			loading : true,
			page : {
				count : "count",
				limit : 10,
				limits : [ 10, 20, 50 ],
				layout : [ 'prev', 'page', 'next', 'skip', 'limit', 'count' ],
				prev : "上一页",
				next : "下一页",
				groups : 5
			},
			cols : [ [ //表头
				{
					field : 'belong',
					title : '地市/区县'
				},
				{
					field : 'name',
					title : '项目名称'
				},
				{
					field : 'stateName',
					title : '项目状态'
				},
				{
					field : 'templateStateName',
					title : '模板状态'
				},
				{
					field : 'stationNum',
					title : '基站数量'
				},
				{
					field : 'dataSources',
					title : 'IT提供的数量'
				},
				{
					field : 'smSendNum',
					title : '待发送数据量'
				}
				,
				{
					field : 'deailNum',
					title : '已发送数据量'
				}
				,
				{
					field : 'reason',
					title : '无发送量原因'
				}
			] ],
			done : function(res) {
				layer.close(index); 
			}
		};

		var source = [];
    		//自动补全下拉
    		 $("#mgrName").keyup(function() {
    			var key = $("#mgrName").val();
    			var cityId=$("#cityId").val();
    			var countyId=$("#countyId").val();
    			var url = '/util/selectMgrNameLike';
    			var param = {
    			    cityId : cityId,
    			    countyId : countyId,
    				key : key
    			};
    			$.ajax({
    				type : 'post',
    				async : false,
    				url : url,
    				data : param,
    				success : function(json) {
    					var data = json.data;
    					for (var i = 0; i < data.length; i++) {
    						source[i] = data[i].name;
    					}
    					console.log(source);
    				},
    				error : function() {
    					layer.msg('系统异常，请联系管理员处理!');
    				}
    			}
    			);
    		}); 
    
    		$("#mgrName").autocomplete({
    			source : function( request, response ) {
    				var key = $("#mgrName").val();
	    			var cityId=$("#cityId").val();
	    			var countyId=$("#countyId").val();
	    			var url = '/util/selectMgrNameLike';
	    			var param = {
	    			    cityId : cityId,
	    			    countyId : countyId,
	    				key : key
	    			};
		               $.ajax({
	    				type : 'post',
	    				async : false,
	    				url : url,
	    				data : param,
	    				success : function(json) {
	    					var data = json.data;
	    					source = [];
	    					for (var i = 0; i < data.length; i++) {
	    						source[i] = data[i].name;
	    					}
	    					console.log(source);
	    					response( source );
	    				},
	    				error : function() {
	    					layer.msg('系统异常，请联系管理员处理!');
	    				}
	    			}
	    			);
	           },
    			messages : { //message设置不出现匹配关键字的结果
    				noResults : '',
    				results : function() {
    				}
    			}
    		});

		//根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //根据城市ID查询项目
            function selectMgrInfo(cityId) {
                var data;
                var url = '/util/findByMgrId?countyId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //监听项目  区县
            form.on('select(cityId)', function (data) {
            	var data2 = selectCountyId(data.value);
                appendCounty(data2);
                var data = selectMgrInfo(data.value);
                appendMgr(data);
            });
            //监听项目
            form.on('select(countyId)', function (data) {
            	var data1;
            	if(null!=data.value && ""!=data.value){
            		data1 = selectMgrInfo(data.value);
            	}else{
            		var cityId = $("#cityId").val();
            		data1 = selectMgrInfo(cityId);
            	}
                appendMgr(data1);
            });
		//动态拼接项目
		function appendMgr(data) {
			if (data.length != null) {
				var options = '<option value="">请选择</option>';
				for (var i = 0; i < data.length; i++) {
					options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
					mgrNameArr.push(data[i].name);
                    mgrIdArr.push(data[i].id);
				}
				$('#mgrId').html(options);
			} else {
				$('#mgrId').html('<option value="">该地市下没有项目</option>');
			}
			form.render();
		}
		//动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value="">请先选择地市</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
            }
		//更新数据表
		$('#query').on('click', function() {
			var mgrId = $("#mgrId").val();
			var cityId = $("#cityId").val();
			var mgrName = $("#mgrName").val();
			var msisdn = $("#msisdn").val();
			var countyId = $("#countyId").val();
			var startTime=$("#startTime").val();
            var endTime=$("#endTime").val();
            console.info(startTime+","+endTime);
			if (
				mgrId == "" && mgrName ==""/* && user.duty == 2 */
			) {
				layer.msg("您必须选择一个项目");
			}
			if (
				mgrId != "" && mgrName !=""/* && user.duty == 2 */
			) {
				layer.msg("项目简称与项目名称只可选择一个");
				return false;
			}
			if(mgrId==''&&mgrName!=''){
            	mgrId=mgrIdArr[mgrNameArr.indexOf(mgrName)];
            } 
			if(!isNull(msisdn)&&!isNull(startTime) && !isNull(endTime)){
				if (startTime>=endTime) {
				    layer.msg("结束时间不能小于等于开始时间");
				    return false;
				}
			}
			
				var index = layer.load(1, {
					shade : [ 0.1, '#fff' ] //0.1透明度的白色背景
				});
				if(null!=msisdn && ""!=msisdn){
					console.info("msisdn"+msisdn);
					table.render(options);
					table.reload('modelReload', {
					method : 'post',
					page : {
						curr : 1
					},
					where : {
						cityId : cityId,
						mgrId : mgrId,
						msisdn : msisdn,
						countyId : countyId,
						startTime:startTime,
						endTime:endTime
					}
					});
				}else{
					table.render(options2);
					table.reload('modelReload2', {
					method : 'post',
					page : {
						curr : 1
					},
					where : {
						cityId : cityId,
						mgrId : mgrId,
						countyId : countyId,
						mgrName:mgrName,
						startTime:startTime,
						endTime:endTime
					}
					});
				}
				layer.close(index);
			
		});
		//身份识别
		$(document).ready(function() {
			$.ajax({
				type : 'post',
				async : false,
				url : '/util/findUser',
				success : function(json) {
					user = json;
					if (json.duty == 1) {
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                			appendCounty(data2); 
                            $("#countyId").val(json.countyId);
                            var data = selectMgrInfo(json.cityId);
                            appendMgr(data);
                            form.render();
                        } else if (json.duty == 2) {
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                			appendCounty(data2); 
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            var data = selectMgrInfo(json.countyId);
                            appendMgr(data);
                            form.render();
                        }else if (json.duty == 3) {
                        	console.info(json);
                        	$("#add").css("visibility","hidden");
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                			appendCounty(data2); 
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            appendMgr(json.list);
                            form.render();
                        }
				},
				error : function() {
					layer.msg('系统异常，请联系管理员处理!');
				}
			});
		});
	});
</script>

</head>
<body>
	<div class="layui-content-body">
		<div class="scroll-box">
			<div class="breadcrumb-box">
				<span class="layui-breadcrumb" lay-separator="|"> <a
					href="javascript:returnIndex2()">首页&nbsp;</a> <a href="#">短信项目管理</a>
					<a href="#">短信项目维护</a>
				</span>
			</div>
			<div class="content-box">
				<div class="form-select-box">
					<div class="layui-form layui-form-pane">
						<div class="layui-form-item">
							<div class="layui-inline">
								<label class="layui-form-label">地市</label>
								<div class="layui-input-block" style="width:110px">
									<select name="cityId" lay-filter="cityId" id="cityId">
										<option value="">请选择</option>
										<option value="11">苏州</option>
										<option value="12">淮安</option>
										<option value="13">宿迁</option>
										<option value="14">南京</option>
										<option value="15">连云港</option>
										<option value="16">徐州</option>
										<option value="17">常州</option>
										<option value="18">镇江</option>
										<option value="19">无锡</option>
										<option value="20">南通</option>
										<option value="21">泰州</option>
										<option value="22">盐城</option>
										<option value="23">扬州</option>
										<option value="25">省级</option>
									</select>
								</div>
							</div>
							<div class="layui-inline countyId">
		                            <label class="layui-form-label">区县</label>
		                            <div class="layui-input-block" style="width:110px">
		                                <select name="countyId" lay-filter="countyId" id="countyId">
		                                    <option value="">全部</option>
		                                </select>
		                            </div>
		                	</div>
							<div class="layui-inline">
								<label class="layui-form-label">项目名称</label>
								<div class="layui-input-block">
									<select name="interest" lay-filter="mgrId" id="mgrId">
										<option value="">请先选择地市</option>
									</select>
								</div>
							</div>
							<div id="shortname" class="layui-inline">
								<label class="layui-form-label">项目简称</label>
								<div class="layui-input-block">
									<input type="text" name="mgrName" lay-verify="required" placeholder="请输入"
										id="mgrName" autocomplete="off" class="layui-input">
								</div>
							</div>
							<div id="shortname" class="layui-inline">
								<label class="layui-form-label">手机号码</label>
								<div class="layui-input-block">
									<input type="text" name="msisdn" lay-verify="required" style="width:200px;"  placeholder="请输入"
										id="msisdn" autocomplete="off" class="layui-input">
								</div>
								<div class="layui-inline" style="color: red">查询号码信息只可以查到今天和昨天的数据（原因：IT数据来源只保存一天）注:不填写时间,默认当天
                                </div>
							</div>
							<div id="time" class="layui-block">
								<label class="layui-form-label">时间段</label>
								<div class="layui-input-inline">
                                <input type="text" name="date" id="startTime" autocomplete="off" class="layui-input"
                                       placeholder="请选择开始时间" lay-verify="required" style="width:180px">
	                            </div>
	                            <div class="layui-form-mid">-</div>
	                            <div class="layui-input-inline">
	                                <input type="text" name="date" id="endTime" autocomplete="off" class="layui-input"
	                                       placeholder="请选择结束时间" lay-verify="required" style="width:180px">
	                            </div>
							</div>
							<div class="layui-inline" pane="">
								<button class="layui-btn layui-btn-normal" id="query">查&nbsp;询</button>
							</div>
						</div>
					</div>
				</div>
				<div class="layui-table">
					<table id="table" lay-filter="tableElement"></table>
				</div>
			</div>
		</div>
	</div>
</body>
</html>